package com.yami.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserCancel;
import com.yami.shop.dao.UserCancelMapper;
import com.yami.shop.service.UserCancelService;
import com.yami.shop.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserCancelServiceImpl extends ServiceImpl<UserCancelMapper, UserCancel> implements UserCancelService {

    @Autowired
    private UserService userService;

    @Override
    public void saveCancelData(String userId) {
        User userDetail = userService.getUserDetail(userId);
        UserCancel userCancel = BeanUtil.copyProperties(userDetail, UserCancel.class);
        this.save(userCancel);
    }

    @Override
    public Boolean isConformToRegister(String mobile, String idNumber) {
        QueryWrapper<UserCancel> userCancelQueryWrapper = new QueryWrapper<>();
        userCancelQueryWrapper.eq(ObjectUtil.isNotEmpty(mobile),"user_mobile",mobile);
        userCancelQueryWrapper.eq(ObjectUtil.isNotEmpty(idNumber),"id_number",idNumber);
        userCancelQueryWrapper.last(" limit 1");
        UserCancel one = this.getOne(userCancelQueryWrapper);
        if(ObjectUtil.isEmpty(one)){
            return true;
        }
        long between = DateUtil.between(one.getCreateTime(), new Date(), DateUnit.DAY);
        return between > 180;
    }
}
